//
//  XFActiveManager.m
//  XFActiveView
//
//  Created by ukongm on 17/6/29.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFActiveManager.h"

@interface XFActiveView : UIView<XFActiveManagerDelegate>

/** 活动提示框 */
@property(nonatomic,strong)UIActivityIndicatorView* indicatorView;

/** 展示图片的提示 */
@property(nonatomic,strong)UIButton* btn;

@end

@implementation XFActiveView

- (UIActivityIndicatorView *)indicatorView{
    
    if (_indicatorView == nil) {
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        
        _indicatorView.frame = CGRectMake(0, 5, self.frame.size.width, self.frame.size.height);
        _indicatorView.center = CGPointMake(self.frame.size.width/2, _indicatorView.center.y);
    }
    return _indicatorView;
}

- (UIButton *)btn{
    
    if (_btn == nil) {
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
        
        [_btn setImage:[UIImage imageNamed:@"ch@3x"] forState:UIControlStateNormal];
        
        [_btn setImage:[UIImage imageNamed:@"dg@3x"] forState:UIControlStateSelected];
        
        _btn.center = CGPointMake(self.frame.size.width/2, _btn.center.y);
        
        _btn.hidden = YES;
    }
    return _btn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.indicatorView];
        [self addSubview:self.btn];
    }
    return self;
}

- (void)start{
    [self changeShow:NO];
    [self.indicatorView startAnimating];
}

- (void)stop{
    [self.indicatorView stopAnimating];
}


- (void)success{
    
    [self changeShow:YES];
    
    self.btn.selected = YES;
}

- (void)fail{
    [self changeShow:YES];
    
    self.btn.selected = NO;
}


// yES 显示按钮 ，  NO 显示活动提示框
- (void)changeShow:(BOOL)show{
    
    self.btn.hidden = !show;
    self.indicatorView.hidden = show;
}


@end


@interface XFActiveManager ()


/** 底层 */
@property(nonatomic,strong)UIView* bottomView;
/** 透明层 */
@property(nonatomic,strong)UIView* backView;
/** 文字 */
@property(nonatomic,strong)UILabel* valueLb;



/** 关联的视图，用来控制交互 */
@property(nonatomic,strong)UIView* superView;


@end

@implementation XFActiveManager

@synthesize activeView = _activeView;

- (UIView *)bottomView{
    
    if (_bottomView == nil) {
        
        CGFloat width = SHOW_WIDTH;
        CGFloat height = SHOW_HEIGHT;
        
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        
        _bottomView.layer.cornerRadius = 5;
        _bottomView.layer.masksToBounds = YES;
    }
    return _bottomView;
}

- (UIView *)backView{
    
    if (_backView == nil) {
        _backView = [[UIView alloc] initWithFrame:self.bottomView.bounds];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0.8;
    }
    return _backView;
}

- (UILabel *)valueLb{
    
    if (_valueLb == nil) {
        CGFloat height = 40;
        _valueLb = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bottomView.frame.size.height - height, self.bottomView.frame.size.width, height)];
        
        _valueLb.textColor = [UIColor whiteColor];
        _valueLb.font = [UIFont systemFontOfSize:14];
        _valueLb.textAlignment = NSTextAlignmentCenter;
    }
    return _valueLb;
}

- (instancetype)init{
    
    self = [super init];
    if (self) {
        
        [self.bottomView addSubview:self.backView];
        [self.bottomView addSubview:self.valueLb];
        
    }
    return self;
}

- (void)setActiveView:(UIView<XFActiveManagerDelegate> *)activeView{
    
    
    if (_activeView) {
        [_activeView removeFromSuperview];
    }
    
    if (activeView == nil) {
        //默认的
        XFActiveView* view = [[XFActiveView alloc] initWithFrame:CGRectMake(0, 0, ACTIVE_SHOW_WIDTH,  ACTIVE_SHOW_HEIGHT)];
        
        activeView = view;
    }
    
    activeView.center = CGPointMake(self.bottomView.frame.size.width/2, (self.bottomView.frame.size.height - self.valueLb.frame.size.height)/2);
    
    _activeView = activeView;
    
    
    [self.bottomView addSubview:self.activeView];
}

- (UIView<XFActiveManagerDelegate> *)activeView{
    if (_activeView == nil) {
        
        [self setActiveView:nil];
        
    }
    return _activeView;
}


- (void)setStatus:(XFActiveManagerStatus)status{
    
    if (_status == status) {
        return;
    }
    
    switch (status) {
        case XFActiveManagerStatusNone:{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.2 animations:^{
                    
                    self.bottomView.transform = CGAffineTransformMakeScale(0.001, 0.001);
                    
                } completion:^(BOOL finished) {
                    
                    [self.activeView stop];
                    [self.bottomView removeFromSuperview];
                    self.bottomView.transform = CGAffineTransformIdentity;
                    self.superView.userInteractionEnabled = YES;
                }];
            });
            
            break;
        }
        case XFActiveManagerStatusActive:{
            
            self.superView.userInteractionEnabled = NO;
            [self.activeView start];
            
            self.bottomView.transform = CGAffineTransformMakeScale(0, 0);
            
//            _status = XFActiveManagerStatusAnimationing;
            
            [UIView animateWithDuration:0.2 animations:^{
               
                self.bottomView.transform = CGAffineTransformIdentity;
                
            } ];
            
            break;
        }
        case XFActiveManagerStatusActiveCompleteSuccess:{
            
            if (_status != XFActiveManagerStatusActive) {
                return;
            }
            
            _status = status;
            
            //判断是否实现了该方法
            if ([self.activeView respondsToSelector:@selector(success)]) {
                [self.activeView success];
            }
            
            [self setStatus:XFActiveManagerStatusNone];
            
            return;
        }
        case XFActiveManagerStatusActiveCompleteFail:{
            
            if (_status != XFActiveManagerStatusActive) {
                return;
            }
            
            _status = status;
            
            //判断是否实现了该方法
            if ([self.activeView respondsToSelector:@selector(fail)]) {
                [self.activeView fail];
            }
            
            [self setStatus:XFActiveManagerStatusNone];
            return;
        }
        default:
            break;
    }
    
    _status = status;
}

- (void)setValue:(NSString*)value norValue:(NSString*)norValue{
    if (!value) {
        value = norValue;
    }
    
    self.valueLb.text = value;
}

- (void)showInView:(UIView *)superView{
    [self showInView:superView value:nil];
}

- (void)showInView:(UIView *)superView value:(NSString *)value{
    
    typeof(self)sself = self;
    
    self.superView = superView;
    
    [superView addSubview:sself.bottomView];
    
    sself.bottomView.center = CGPointMake(superView.frame.size.width/2, superView.frame.size.height/2);
    
    [sself setStatus:XFActiveManagerStatusActive];
    [sself setValue:value norValue:XFSTATUS_ACTIVITING];
}

- (void)stopActive{
    typeof(self)sself = self;
       
    [sself setStatus:XFActiveManagerStatusNone];
}

- (void)success{
    [self successForValue:nil];
}

- (void)successForValue:(NSString *)value{
    
    typeof(self)sself = self;
    
    [sself setStatus:XFActiveManagerStatusActiveCompleteSuccess];
        
    [sself setValue:value norValue:XFSTATUS_COMPLETE_SUCCESS];
}

- (void)fail{
    [self failForValue:nil];
}

- (void)failForValue:(NSString *)value{

    typeof(self)sself = self;
    
    [sself setStatus:XFActiveManagerStatusActiveCompleteFail];
        
    [sself setValue:value norValue:XFSTATUS_COMPLETE_FAIL];
}



@end

















